package yl.hs.bmipfull.controllers.production;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.production.IProductDefectiveService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/**
 * 作用:  不良品出入库记录控制层<br/>
 * 创建时间: 2023年08月12日 09时31分37秒 <br/>
 *
 * @author 吴高丰
 */
@Api(value = "不良品出入库记录Web接口", tags = "管理--不良品出入库记录操作")
@RequestMapping(value = "/api/product_defective")
@RestController
public class ProductDefectiveController extends ControllerBase {

    @Autowired
    IProductDefectiveService service;

    @ApiOperation(value = "查询不良品出入库记录", notes = "查询不良品出入库记录")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return fail("查询失败,查询条件异常!");
        }
    }

    @ApiOperation(value = "查询不良品出入库记录", notes = "分页查询不良品出入库记录")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询不良品出入库记录", notes = "根据不良品出入库记录编号查询不良品出入库记录")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存不良品出入库记录", notes = "查询/更新不良品出入库记录信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveproduct_defective(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else if(result==-1){
                return fail("库存不足");
            }else{
                return fail("数据未保存");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return fail("数据未保存");
        }

    }

    @ApiOperation(value = "删除不良品出入库记录信息", notes = "根据不良品出入库记录编号删除系不良品出入库记录信息")
    @RequestMapping(path = "/{sn}", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "不良品出入库记录编号", name = "sn", allowMultiple = true, required = true) @PathVariable("sn") String sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
}